package yznu_Servlet_login;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import yznu_Bean_login.Login_Database;
//��¼ser
public class LoginSer extends HttpServlet {
String Msg = "";
String name = "";
String Username = "";
String Password = "";
String Type = "";
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
			Username = request.getParameter("Username");
			Password = request.getParameter("Password");
			Msg = isSuccess(Username,Password);
			if(Msg.equals("成功")){
				HttpSession session = request.getSession(true);
				session.setAttribute("User", Username);
				
				if(name.equals("null")){
					session.setAttribute("name", Username);
				}else
					session.setAttribute("name", name);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/main.jsp");
				 dispatcher.forward(request, response);
			}else{
				request.setAttribute("Msg",Msg);
				RequestDispatcher dispatcher=request.getRequestDispatcher("admin.jsp");
				 dispatcher.forward(request, response);
			}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=gb2312");
		doGet(request, response);
	}
	
	public String isSuccess(String Username,String Password){
			name = new Login_Database().AdminCheck(Username, Password);
			if(name.equals("error")){
				Msg = "管理员不存在!";
			}else
				Msg = "成功";
		return Msg; 
	}
}
